Add Validation to Order Creation#1038
Conversation
pollygee
left a comment
There was a problem hiding this comment.
Thanks @Josiassejod1 for your work here.
I don't think this current implementation is working to do the validations we need, so I've added some questions and comments. Check them out and let me know what you think If you have questions feel free to hit me up on slack!
Thanks again for your effort, we're on the right track and with some small updates this will be working just as needed.
| order.sufficient_shares? | ||
| end | ||
|
|
||
| true |
There was a problem hiding this comment.
because this is outside of the if block this function will always return true and that isn't what we want we'd want it to return the result of sufficient_funds or sufficient_shares.
There was a problem hiding this comment.
good callout I need to call return
app/models/order.rb
Outdated
| return true unless number_of_shares_valid_for_calculations? | ||
|
|
||
| current_shares = user.portfolio&.shares_owned(stock_id) || 0 | ||
| shares <= current_shares |
There was a problem hiding this comment.
Same here, we'll need to add an error if there aren't enough shares not just return false so we have an indication of why it didn't work.
There was a problem hiding this comment.
This makes sense to me!
app/models/order.rb
Outdated
|
|
||
| current_balance_cents = (user.portfolio&.cash_balance || 0) * 100 | ||
|
|
||
| fee_already_subtracted = user.orders.pending.exists? ? PortfolioTransaction::TRANSACTION_FEE_CENTS : 0 |
There was a problem hiding this comment.
I think the purpose of this function here here is not calculating "if" the fee was already calculated but rather what fee we need to add
maybe a better name could be fee_to_charge
| order.save! | ||
| end | ||
|
|
||
| assert order.errors[:shares].first.include?("Insufficient funds") |
There was a problem hiding this comment.
Same question as below, is this the error message we will get?
| order.save! | ||
| end | ||
|
|
||
| assert order.errors[:base].first.include?("Cannot sell more shares than you own") |
There was a problem hiding this comment.
Is this the error message we'd get I don't see it added as an error when we do the check.
app/models/order.rb
Outdated
|
|
||
| total_needed = purchase_cost + transaction_fee | ||
|
|
||
| balance_without_reservations >= total_needed |
There was a problem hiding this comment.
We need to add an error here to alert the user to why the transaction fails if it does fail.
Can you add something like:
errors.add(:shares, "Insufficient funds. You have #{formatted_balance} but need #{formatted_cost}")?
|
Thanks for the clarity ! |
|
@AbuMareBear do you have any availability any time soon to take a look at these updates to the PR? |
pollygee
left a comment
There was a problem hiding this comment.
Thanks for your work on this I think we're closer.
I still have some concerns here. We do already have validations on order for when an order is created.
This order execution happens in a job that runs at night so it isn't user facing. I think we might be able to reuse some of that work that has been done to validate enough stocks and cash to buy?
Feel free to ping me in slack if you want to talk through this a little more.
| current_balance_cents = (user.portfolio&.cash_balance || 0) * 100 | ||
|
|
||
| fee_to_charge = user.orders.pending.exists? ? PortfolioTransaction::TRANSACTION_FEE_CENTS : 0 | ||
| balance_without_reservations = current_balance_cents + purchase_cost + fee_to_charge |
There was a problem hiding this comment.
I'm confused with this line here, I'm not sure what this is calculating. How is the balance something we're getting from adding the purchase cose and fees to be charged?
|
|
||
| return true if shares <= current_shares | ||
|
|
||
| formatted_shares = (current_shares % 1).zero? ? current_shares.to_i : current_shares |
There was a problem hiding this comment.
I don't think we need this formatting because this is being run by a job so the user will not see an error message here at all.
Pull Request
Summary
There isn't current validation on
ExecuteOrderwhen there are insufficient funds or shares, thus, this PR adds some tests and checks.Related Issue
Resolves #1010
Changes
Screenshots (if applicable)
Checklist
Notes